package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.util.ImageHelper;

/**
 * Created by Administrator on 2017/9/23 0023.
 */

public class StickerIndicatorView extends View {

    //选中的位置
    private int mSelectIndex;

    private Bitmap mBg;

    //当前移动到哪 左边
    private float mCurrentLength;

    private Paint mPaint;

    private int mViewHeight;

    private int mIndicatorWidth;

    private RectF mLeftRect;
    private RectF mRightRect;


    //选中区域大小
    public static final float BOX_WIDTH = ComponentContext.getContext().getResources().getDimension(R.dimen.image_eidt_emoji_bar_width);
    private Bitmap mIndicatorBitmap;
    private PorterDuffXfermode mPorterDuffXfermode;
    private Rect mSrcRect;
    private Rect mDestRect;

    public StickerIndicatorView(Context context) {
        this(context, null);
    }

    public StickerIndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StickerIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.white_top_mask_color));

        mIndicatorBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_indicator_down);
        mViewHeight = mIndicatorBitmap.getHeight();
        mIndicatorWidth = mIndicatorBitmap.getWidth();

        mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

        mDestRect = new Rect(0, 0, ImageHelper.SCREEN_WIDTH, mViewHeight);


        mSelectIndex = 0;
        mCurrentLength = BOX_WIDTH / 2 - mIndicatorWidth / 2;
        mLeftRect = new RectF(0, 0, mCurrentLength, mViewHeight);
        mRightRect = new RectF(mCurrentLength + mIndicatorWidth, 0, ImageHelper.SCREEN_WIDTH, mViewHeight);

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(ImageHelper.SCREEN_WIDTH, mViewHeight);
    }

    public void moveToIndex(int index) {
        if (index == mSelectIndex) {
            return;
        }
        mCurrentLength += BOX_WIDTH * (index - mSelectIndex);
        mLeftRect.set(0, 0, mCurrentLength, mViewHeight);
        mRightRect.set(mCurrentLength + mIndicatorWidth, 0, ImageHelper.SCREEN_WIDTH, mViewHeight);
        mSelectIndex = index;
        invalidate();
    }

    public void moveTo(int index, float length) {
        mCurrentLength = length + BOX_WIDTH / 2 - mIndicatorWidth / 2;
        mLeftRect.set(0, 0, mCurrentLength, mViewHeight);
        mRightRect.set(mCurrentLength + mIndicatorWidth, 0, ImageHelper.SCREEN_WIDTH, mViewHeight);
        mSelectIndex = index;
        invalidate();
    }

    public void moveByDistance(float distance) {
        mCurrentLength += distance;
        mLeftRect.set(0, 0, mCurrentLength, mViewHeight);
        mRightRect.set(mCurrentLength + mIndicatorWidth, 0, ImageHelper.SCREEN_WIDTH, mViewHeight);
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBg != null) {
            canvas.drawBitmap(mBg, mSrcRect, mDestRect, mPaint);
            mPaint.setXfermode(mPorterDuffXfermode);
            canvas.drawBitmap(mIndicatorBitmap, mCurrentLength, 0, mPaint);
            mPaint.setXfermode(null);
            canvas.drawRect(mLeftRect, mPaint);
            canvas.drawRect(mRightRect, mPaint);
        } else {
            mPaint.setAlpha(204);
            canvas.drawBitmap(mIndicatorBitmap, mCurrentLength, 0, mPaint);
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mBg != null && !mBg.isRecycled()) {
            mBg.recycle();
        }
    }

    public void setBg(Bitmap bg) {
        if (bg == null || bg.isRecycled()) {
            return;
        }
        Matrix matrix = new Matrix();
        matrix.setScale(1, -1);
        mBg = Bitmap.createBitmap(bg, 0, 0, bg.getWidth(), bg.getHeight(), matrix, false);
        if (mSrcRect == null) {
            mSrcRect = new Rect(0, 0, mBg.getWidth(), mViewHeight);
        }
        invalidate();
    }
}
